﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using System.IO;
namespace AnyOffice.general.knowledge
{
    public partial class tindex : ModulePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            //DataTable table = new KnowledgeDao().SelectAll(base.Conn);
            //UserCache cache = new UserCache();
            //DataView defaultView = table.DefaultView;
            //string str = "(Type = 1)";
            //str = ((((((str + " OR ((Type = 2) AND (',' + PostDept LIKE '%," + cache.DeptId.ToString() + ",%'))") + " OR ((Type = 3) AND (',' + AccessUser LIKE '%," + base.User.Identity.Name + ",%'))") + " OR (',' + NewUser LIKE '%," + base.User.Identity.Name + ",%')") + " OR (',' + ReadUser LIKE '%," + base.User.Identity.Name + ",%')") + " OR (',' + EditUser LIKE '%," + base.User.Identity.Name + ",%')") + " OR (',' + DownloadUser LIKE '%," + base.User.Identity.Name + ",%')") + " OR (',' + ManageUser LIKE '%," + base.User.Identity.Name + ",%')";
            //defaultView.RowFilter = str;
            //if (defaultView.Count == 0)
            //{
            //    this.litMessage.Text = base.ShowMessage("无知识目录");
            //}
            //else
            //{
            //    this.rptKnowledge.DataSource = defaultView;
            //    this.rptKnowledge.DataBind();
            //}
            if (!IsPostBack)
            {
                Bindtree(tree_Path.Nodes);
            }
        }

        #region 树生成



        /// <summary>
        /// 节点绑定
        /// </summary>
        public void Bindtree(TreeNodeCollection Nds)
        {

            DataTable table = new KnowledgeDao().SelectAll(base.Conn);
            UserCache cache = new UserCache();
            DataView defaultView = table.DefaultView;
            string str = "(Type = 1)";
            str = ((((((str + " OR ((Type = 2) AND (',' + PostDept LIKE '%," + cache.DeptId.ToString() + ",%'))") + " OR ((Type = 3) AND (',' + AccessUser LIKE '%," + base.User.Identity.Name + ",%'))") + " OR (',' + NewUser LIKE '%," + base.User.Identity.Name + ",%')") + " OR (',' + ReadUser LIKE '%," + base.User.Identity.Name + ",%')") + " OR (',' + EditUser LIKE '%," + base.User.Identity.Name + ",%')") + " OR (',' + DownloadUser LIKE '%," + base.User.Identity.Name + ",%')") + " OR (',' + ManageUser LIKE '%," + base.User.Identity.Name + ",%')";
            defaultView.RowFilter = str;
            if (defaultView.Count == 0)
            {
                this.litMessage.Text = base.ShowMessage("无知识目录");
                return;
            }

            foreach (DataRowView item in defaultView)
            {


                //获取文件路径
                string path = Server.MapPath("/attachment/knowledge/diskpath" + item["DiskId"].ToString());

                TreeNode pnode = new TreeNode();
                pnode.Value = item["DiskId"].ToString();
                pnode.Text = item["Name"].ToString();
                pnode.ImageUrl = "/images/files/folder.gif";
                Nds.Add(pnode);

                GetFiles(path, pnode);

            }



        }


        private void GetFiles(string filePath, TreeNode node)
        {


            DirectoryInfo folder = new DirectoryInfo(filePath);
            node.Text = string.Empty.Equals(node.Text) ? folder.Name : node.Text;
            node.Target = folder.FullName;
            node.ImageUrl = "/images/files/folder.gif";


            //FileInfo[] chldFiles = folder.GetFiles("*.*");
            //foreach (FileInfo chlFile in chldFiles)
            //{
            //    TreeNode chldNode = new TreeNode();
            //    chldNode.Text = chlFile.Name;
            //    chldNode.Tag = chlFile.FullName;
            //    node.Nodes.Add(chldNode);
            //}

            DirectoryInfo[] chldFolders = folder.GetDirectories();
            foreach (DirectoryInfo chldFolder in chldFolders)
            {
                TreeNode chldNode = new TreeNode();
                chldNode.Text = chldFolder.Name;
                chldNode.Target = chldFolder.FullName;
                chldNode.Value = chldFolder.Name; ;
                chldNode.ImageUrl = "/images/files/folder.gif";
                node.ChildNodes.Add(chldNode);
                GetFiles(chldFolder.FullName, chldNode);
            }

        }
        #endregion


        /// <summary>
        /// 保存当前顶层根目录
        /// </summary>
        protected void tree_Path_SelectedNodeChanged(object sender, EventArgs e)
        {
            //if (tree_Path.SelectedValue != "")
            //{

            //    //access.aspx?disk_id=<%#Eval("DiskId")%>

            //    Model.Disk d = bll.GetModel(base.Conn, Convert.ToInt64(tree_Path.SelectedValue));
            //    if (d.ParentDiskID == 0)//是否到顶层
            //    {
            //        Session["homeDiskID"] = tree_Path.SelectedValue; //当前根目录
            //    }
            //    else
            //    {
            //        //获得跟节点
            //        int id = bll.GetLeveID(base.Conn, tree_Path.SelectedValue);
            //        if (id != -1)
            //        {
            //            Session["homeDiskID"] = id;
            //        }

            //    }
            //    Session["Cdid"] = tree_Path.SelectedValue;
            //    labCurent.Text = bll.GetParentPath(base.Conn, tree_Path.SelectedValue, "");
            //    Bindrep(tree_Path.SelectedValue, "");
            //}
        }
    }
}
